/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.timer.creation;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.mopore.jat.gui.IntegerTextField;
import org.mopore.jat.gui.SelectTextFocusAdapter;
import org.mopore.jat.gui.wizard.AbstractWizardPage;
import org.mopore.jat.gui.wizard.ButtonPanel;
import org.mopore.jat.gui.wizard.WizardContext;

public class CustomCountDownPage extends AbstractWizardPage<TimingCreateProgress> {
	
	
	private IntegerTextField secondsTextField;
	private IntegerTextField minutesTextField;
	private IntegerTextField hoursTextField;
	private IntegerTextField daysTextField;


	public CustomCountDownPage() {
		
		ButtonPanel buttonPanel = getButtonPanel();
		buttonPanel.enableBackButton();
		buttonPanel.addActionToNextButton( new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Class<? extends AbstractWizardPage<TimingCreateProgress>> finishPage = 
					FinishPage.class;
				changePage( finishPage  );
			}
		} );
		buttonPanel.addActionToBackButton( new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Class<? extends AbstractWizardPage<TimingCreateProgress>> startPage = 
					StartPage.class;
				changePage( startPage  );
			}
		} );
	}


	@Override
	public JPanel getContentPanel(){
		
		ButtonPanel buttonPanel = getButtonPanel();
		CustomCountDownValidator validator = 
			new CustomCountDownValidator( buttonPanel );
		
		JPanel panel = new JPanel();
		
		GridLayout gridLayout = new GridLayout( 4, 2 );
		panel.setLayout( gridLayout );
		
		JLabel secondsLabel = new JLabel( "seconds:" );
		secondsTextField = new IntegerTextField();
		validator.addField( secondsTextField );
		{ 
			FocusListener focusListener = 
				new SelectTextFocusAdapter( secondsTextField );
			secondsTextField.addFocusListener( focusListener );
		}
		secondsLabel.setLabelFor( secondsTextField );
		panel.add( secondsLabel );
		panel.add( secondsTextField );
		
		JLabel minutesLabel = new JLabel( "minutes:" );
		minutesTextField = new IntegerTextField();
		validator.addField( minutesTextField );
		{ 
			FocusListener focusListener = 
				new SelectTextFocusAdapter( minutesTextField );
			minutesTextField.addFocusListener( focusListener );
		}		
		minutesLabel.setLabelFor( minutesTextField );
		panel.add( minutesLabel );
		panel.add( minutesTextField );
		
		JLabel hoursLabel = new JLabel( "hours:" );
		hoursTextField = new IntegerTextField();
		validator.addField( hoursTextField );
		{ 
			FocusListener focusListener = 
				new SelectTextFocusAdapter( hoursTextField );
			hoursTextField.addFocusListener( focusListener );
		}	
		hoursLabel.setLabelFor( hoursTextField );
		panel.add( hoursLabel );
		panel.add( hoursTextField );		
		
		JLabel daysLabel = new JLabel( "days:" );
		daysTextField = new IntegerTextField();
		validator.addField( daysTextField );
		{ 
			FocusListener focusListener = 
				new SelectTextFocusAdapter( daysTextField );
			daysTextField.addFocusListener( focusListener );
		}			
		daysLabel.setLabelFor( daysTextField );
		panel.add( daysLabel );
		panel.add( daysTextField );		
		
		panel.setBorder( BorderFactory.createEmptyBorder( 5, 30, 5, 30) );
		
		applyContextValues();
		
		return panel;
	}
	
	
	private void applyContextValues() {
		
		boolean freeButtons = false;
		
		WizardContext<TimingCreateProgress> context = context();
		TimingCreateProgress progress = context.get();
		
		Integer seconds = progress.getSeconds();
		if ( seconds != null ){
			secondsTextField.setIntegerValue( seconds );
			freeButtons = true;
		}
		
		Integer minutes = progress.getMinutes();
		if ( minutes != null ){
			minutesTextField.setIntegerValue( minutes );
			freeButtons = true;
		}
		
		Integer hours = progress.getHours();
		if ( hours != null ){
			hoursTextField.setIntegerValue( hours );
			freeButtons = true;
		}
	
		Integer days = progress.getDays();
		if ( days != null ){
			daysTextField.setIntegerValue( days );
			freeButtons = true;
		}
		
		if ( freeButtons ){
			ButtonPanel buttonPanel = getButtonPanel();
			buttonPanel.enableNextButton();
			buttonPanel.enableFinishButton();
		}
	}


	@Override
	public void storeToContext() {
		
		WizardContext<TimingCreateProgress> context = context();
		TimingCreateProgress progress = context.get();
		
		Integer seconds = secondsTextField.getIntegerValue();
		progress.setSeconds( seconds );
		
		Integer minutes = minutesTextField.getIntegerValue();
		progress.setMinutes( minutes );
		
		Integer hours = hoursTextField.getIntegerValue();
		progress.setHours( hours );
	
		Integer days = daysTextField.getIntegerValue();
		progress.setDays( days );		
	}
	

}
